package com.loong.common.utils;

import com.loong.common.domain.Pager;
import com.loong.common.function.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 类型之间的转化
 */
public class TransformUtils {

    public static <T> void  transform(Object source,T target){
        BeanUtils.copyProperties(source,target);
    }

    /**
     * 对象转换工具
     * @param source 源数据
     * @param tClass 目标对象类型
     * @param <T>
     * @return
     */
    public  static <T> T  transform(Object source,Class<T> tClass){
        T t = null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(source,t);
        return t;
    }
    public static <T> List<T> transformList(Collection source,Class<T> tClass){
        return (List<T>) source.stream().map((Function<Object, T>) o -> transform(o, tClass)).collect(Collectors.toList());
    }

    public static <T> Pager<T> transformPage(Pager source, Class<T> tClass){
        List dataList = source.getDataList();
        List<T> ts = transformList(dataList, tClass);
        source.setDataList(ts);
        return source;
    }

}
